package com.unknowpupil.web.config.version;

import tk.mybatis.mapper.version.NextVersion;
import tk.mybatis.mapper.version.VersionException;

import java.sql.Timestamp;

/**
 * @Classname: DefaultNextVersion
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2019/10/8 15:39
 * @Description: 乐观锁
 * @Version: 1.0
 */
public class DefaultNextVersion  implements NextVersion {

    @Override
    public Object nextVersion(Object current) throws VersionException {
        if (current == null) {
            return  0;
//            throw new VersionException("当前版本号为空!");
        }
        if (current instanceof Integer) {
            return (Integer) current + 1;
        }
        else if (current instanceof Long) {
            return (Long) current + 1L;
        }
        else if (current instanceof Timestamp) {
            return new Timestamp(System.currentTimeMillis());
        }
        else {
            throw new VersionException("默认的 NextVersion 只支持 Integer, Long" +
                    " 和 java.sql.Timestamp 类型的版本号，如果有需要请自行扩展!");
        }
    }

}
